package com.andy.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.andy.dao.IExamSubjectDAO;
import com.andy.dao.IExamTestDAO;
import com.andy.dao.IExamTestTypeDAO;
import com.andy.entity.ExamSubject;
import com.andy.entity.ExamTest;
import com.andy.entity.ExamTestType;
import com.andy.utils.TestUtils1;
import com.andy.vo.ExamTestTypeVO;
import com.andy.vo.ExamTestVO;

@Service
@Transactional
public class TestService1 implements ITestService1 {

	@Autowired
	private IExamSubjectDAO examSubjectDAO;
	
	@Autowired
	private IExamTestDAO examTestDAO;
	
	@Autowired
	private IExamTestTypeDAO examTestTypeDAO;
	
	@Override
	public Map<Integer, List<ExamTestVO>> getTestListBySubject(Integer subId) {
		ExamSubject currentSubject = examSubjectDAO.get(subId);
		if (currentSubject != null) {
			List<ExamTestType> testTypeList = examTestTypeDAO.loadAll();
			Map<Integer, Integer> testTypeScoreMap = new HashMap<Integer, Integer>();
			for (ExamTestType testType : testTypeList) {
				testTypeScoreMap.put(testType.getId(), testType.getTestTypeScore());
			}
			Map<Integer, Integer> testTypeCountMap = TestUtils1.caculateTestTypeCount(currentSubject.getTestCount(), currentSubject.getTotalScore(), testTypeScoreMap);
			if (testTypeCountMap != null) {
				Map<Integer, List<ExamTestVO>> resultMap = new HashMap<Integer, List<ExamTestVO>>();
				List<Integer> multiTotalList = examTestDAO.getTestIdByType(subId, TestUtils1.MULTI_TEST_TYPE);
				List<Integer> singleTotalList = examTestDAO.getTestIdByType(subId, TestUtils1.SINGLE_TEST_TYPE);
				if (testTypeCountMap.get(TestUtils1.MULTI_TEST_TYPE) != null) {
					List<Integer> multiResultIdList = TestUtils1.getRandomList(multiTotalList, testTypeCountMap.get(TestUtils1.MULTI_TEST_TYPE));
					if (multiResultIdList != null && multiResultIdList.size() > 0) {
						List<ExamTest> totalList = examTestDAO.getTestListByIds(multiResultIdList);
						List<ExamTestVO> resultTotalList = new ArrayList<ExamTestVO>();
						for (ExamTest test : totalList) {
							ExamTestVO vo = new ExamTestVO();
							BeanUtils.copyProperties(test, vo);
							
							ExamTestTypeVO typeVO = new ExamTestTypeVO();
							BeanUtils.copyProperties(test.getExamTestType(), typeVO);
							vo.setExamTestType(typeVO);
							resultTotalList.add(vo);
						}
						resultMap.put(TestUtils1.MULTI_TEST_TYPE, resultTotalList);
					} else {
						return null;
					}
				}
				if (testTypeCountMap.get(TestUtils1.SINGLE_TEST_TYPE) != null) {
					List<Integer> singleResultIdList = TestUtils1.getRandomList(singleTotalList, testTypeCountMap.get(TestUtils1.SINGLE_TEST_TYPE));
					if (singleResultIdList != null && singleResultIdList.size() > 0) {
						List<ExamTest> totalList = examTestDAO.getTestListByIds(singleResultIdList);
						List<ExamTestVO> resultTotalList = new ArrayList<ExamTestVO>();
						for (ExamTest test : totalList) {
							ExamTestVO vo = new ExamTestVO();
							BeanUtils.copyProperties(test, vo);
							
							ExamTestTypeVO typeVO = new ExamTestTypeVO();
							BeanUtils.copyProperties(test.getExamTestType(), typeVO);
							vo.setExamTestType(typeVO);
							resultTotalList.add(vo);
						}
						resultMap.put(TestUtils1.SINGLE_TEST_TYPE, resultTotalList);
					}
				}
				return resultMap;
			}
		}
		
		return null;
	}
	
}
